<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="单位" prop="oneInstitutional"
                :rules="[
                 ]">
              <el-select disabled="" v-model="inputForm.oneInstitutional" placeholder="请填写票类别" style="width:100%;" >
                <el-option v-for="item in oneInstitutionalArr" :label="item.organization" :value="item.organization" :key="item.id"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item disabled="" label="单位类别" prop="organizationLevel"
                :rules="[
                 ]">
              <el-select disabled="" v-model="inputForm.organizationLevel" placeholder="请填写票类别" style="width:100%;" >
                <el-option v-for="item in oneInstitutionalArr" :label="item.organizationLevel" :value="item.organizationLevel" :key="item.id"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="票类别" prop="ticket"
                :rules="[
                 ]">
              <el-checkbox-group v-model="ticket">
                <el-checkbox-button v-for="item in tickets" :label="item" :key="item">{{item}}</el-checkbox-button>
              </el-checkbox-group>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col> -->
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          oneInstitutional: '',
          organizationLevel: '',
          ticket: '',
          remarks: '',
          createdate: '',
          createData: '',
          deleteData: '',
          status: ''
        },
        ticket: [],
        tickets: [
          '领导班子(校领导票)',
          '领导班子(同级票)',
          '领导班子(教辅群众票)',
          '领导班子(教学群众票)',
          '领导班子(教辅服务对象票)'
        ],
        oneInstitutionalArr: []
      }
    },
    components: {
    },
    activated () {
      this.searchOneInstitutional()
    },
    methods: {
      init (method, id) {
        this.ticket = []
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建领导班子考核信息`
        } else if (method === 'edit') {
          this.title = '修改领导班子考核信息'
        } else if (method === 'view') {
          this.title = '查看领导班子考核信息'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/peopleinformation/peopleassesteam/peopleAssessTeam/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.peopleAssessTeam)
              for (let i in this.inputForm.ticket.split(',')) {
               if (this.inputForm.ticket.split(',')[i] !== '') {
                 this.ticket.push(this.inputForm.ticket.split(',')[i])
               }
              }
              this.inputForm.createData = this.inputForm.ticket
              this.loading = false
            })
          }
        })
      },
      // 搜索框下拉数据提供 单位名称下拉框数据请求
      searchOneInstitutional () {
        this.$http({
          url: '/peopleinformation/organization/nameOfOrganization/treeData',
          method: 'get'
        }).then(({data}) => {
          this.oneInstitutionalArr = []
          for (let index = 0; index < data.treeData.length; index++) {
            this.oneInstitutionalArr.push(data.treeData[index])
          }
        })
      },
      // 比较插入与删除
      compare () {
        var oldArr = this.inputForm.createData.split(',')
        var newArr = this.inputForm.ticket.split(',')
        var deleteArr = []
        var insertArr = []
        for (let i in oldArr) {
            if (newArr.indexOf(oldArr[i]) === -1 && oldArr[i] !== '') {
              deleteArr.push(oldArr[i])
            }
        }
        for (let i in newArr) {
            if (oldArr.indexOf(newArr[i]) === -1 && newArr[i] !== '') {
              insertArr.push(newArr[i])
            }
        }
        if (insertArr.length !== 0 && deleteArr.length !== 0) {
          this.inputForm.status = 0
        } else if (insertArr.length !== 0) {
          this.inputForm.status = 1
        } else if (deleteArr.length !== 0) {
          this.inputForm.status = 2
        }
        this.inputForm.createData = this.$ticketTableName.tableName(insertArr)
        this.inputForm.deleteData = this.$ticketTableName.tableName(deleteArr)
      },
      // 表单提交
      doSubmit () {
        this.inputForm.createdate = (new Date().getFullYear() + '-')
        this.inputForm.ticket = ''
        for (let i in this.ticket) {
          if (i === '0') {
            this.inputForm.ticket += this.ticket[i]
          } else {
            this.inputForm.ticket += ',' + this.ticket[i]
          }
        }
        this.compare()
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/peopleinformation/peopleassesteam/peopleAssessTeam/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
